package com.t01.bdqn.aop;


import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggerAspect {
    //com.t01.bdqn.service.impl.ComputerRoomServiceImpl.ComputerSet(..)
//    @Around(value = "execution(* com.t01.bdqn.service.impl..*.*(..))")
//    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
//        long start = System.currentTimeMillis();
//        Object object = joinPoint.proceed();
//        long end = System.currentTimeMillis();
//        //获取切入点的方法名
//        System.out.println( joinPoint.getSignature().getName() +"方法执行耗时："+(end-start));
//        return object;
//    }


    @Around(value = "execution(* com.t01.bdqn.service.StudentService.*(..))")
    public Object log(ProceedingJoinPoint joinPoint) throws Throwable {

        System.out.println("aop 往方法："+joinPoint.getSignature().getName() +"编织入代码");
        Object object = joinPoint.proceed();
        return object;
    }

    @AfterThrowing(throwing="ex"
            , pointcut="execution(* com.t01.bdqn.service.StudentService.*(..))")
    // 声明ex时指定的类型会限制目标方法必须抛出指定类型的异常
    // 此处将ex的类型声明为Throwable，意味着对目标方法抛出的异常不加限制
    public void doRecoveryActions(JoinPoint joinPoint, Throwable ex)
    {
        System.out.println(joinPoint.getSignature().getName());
        System.out.println("目标方法中抛出的异常:" + ex);
        System.out.println("模拟Advice对异常的修复...");
    }


}
